package edu.thu.studio.ormdesigner.editor.policy;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import edu.thu.studio.ormdesigner.editor.command.CreateLinkCommand;
import edu.thu.studio.ormdesigner.editor.command.ReconnectLinkCommand;
import edu.thu.studio.ormdesigner.model.diagram.link.Link;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;

public class NodeGraphicalNodeEditPolicy extends GraphicalNodeEditPolicy {
    
    @Override
    protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
        CreateLinkCommand command = (CreateLinkCommand) request.getStartCommand();
        command.setTarget((Node) getHost().getModel());
        return command;
    }
    
    @Override
    protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
        Node model = (Node) getHost().getModel();
        if (model instanceof TableNode) {
            CreateLinkCommand command = new CreateLinkCommand(model);
            request.setStartCommand(command);
            return command;
        }
        return null;
    }
    
    @Override
    protected Command getReconnectSourceCommand(ReconnectRequest request) {
        Link link = (Link) request.getConnectionEditPart().getModel();
        Node newSource = (Node) getHost().getModel();
        ReconnectLinkCommand cmd = new ReconnectLinkCommand(link);
        cmd.setNewSource(newSource);
        return cmd;
    }
    
    @Override
    protected Command getReconnectTargetCommand(ReconnectRequest request) {
        Link link = (Link) request.getConnectionEditPart().getModel();
        Node newSource = (Node) getHost().getModel();
        ReconnectLinkCommand cmd = new ReconnectLinkCommand(link);
        cmd.setNewTarget(newSource);
        return cmd;
    }
    
}
